package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.bean.Employee;
import com.itheima.bean.P;
import com.itheima.dao.EmployeeDao;
import com.itheima.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import javax.servlet.http.HttpSession;
import java.util.List;

@Transactional
@Service
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeDao employeeDao;
    @Override
    public Employee findById(Employee employee) {
        //1.创建设置条件对象,泛型为Employee
        LambdaQueryWrapper<Employee> qw = new LambdaQueryWrapper<>();
        String s = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());
        //2、设置查询条件
        qw.eq(Employee::getUsername,employee.getUsername());
        qw.eq(Employee::getPassword,s);

        return employeeDao.selectOne(qw);
    }

    @Override
    public int add(Employee employee, HttpSession session) {
        //1.创建设置条件对象
        /*LambdaQueryWrapper<Employee> qw = new LambdaQueryWrapper<>();
        //2.设置添加条件
        qw.eq(Employee::getUsername,employee.getUsername());
        Employee employee1 = employeeDao.selectOne(qw);
        if (employee1==null){*/
        Employee e = (Employee) session.getAttribute("employee");
        String s = DigestUtils.md5DigestAsHex("123456".getBytes());
        employee.setPassword(s);
        employee.setStatus(1);
        /*employee.setCreateUser(e.getId());
        employee.setUpdateUser(e.getId());
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());*/
        //调用dao层的添加方法，返回影响行数
        return employeeDao.insert(employee);
        /*}else{
            return -1;
        }*/
    }
    @Override
    public /*IPage<Employee>*/P findPage(int page , int pageSize, String s){
        if (s==null) {
            //创建Ipage接口对象变量，接受实现类Page对象
            IPage<Employee> ip = new Page<>(page, pageSize);
            //创建分页查询对象
        /*QueryWrapper<Employee> qw = new QueryWrapper<>();
        qw.*/
            //当前页数据集合
            List<Employee> list = employeeDao.selectPage(ip, null).getRecords();
            //当前页码
            int p = (int) employeeDao.selectPage(ip, null).getCurrent();
            //当前页得数据总量
            int ps = (int) employeeDao.selectPage(ip, null).getSize();
            //数据总量
            int tobal = (int) employeeDao.selectPage(ip, null).getTotal();
            //一共多少页
            int pages = (int) employeeDao.selectPage(ip, null).getPages();
            //return employeeDao.selectPage(ip,null);
            return new P(list,p,ps,tobal,pages);
        }else {
            IPage<Employee> ip = new Page<>(page, pageSize);
            //创建条件查询对象
            LambdaQueryWrapper<Employee> qw = new LambdaQueryWrapper<>();
            //设置模糊查询得条件
            qw.like(Employee::getName,s);
            //当前页数据集合
            List<Employee> list = employeeDao.selectPage(ip, qw).getRecords();
            //当前页码
            int p = (int) employeeDao.selectPage(ip, qw).getCurrent();
            //当前页得数据总量
            int ps = (int) employeeDao.selectPage(ip, qw).getSize();
            //数据总量
            int tobal = (int) employeeDao.selectPage(ip, qw).getTotal();
            //一共多少页
            int pages = (int) employeeDao.selectPage(ip, qw).getPages();
            //IPage<Employee> employeeIPage = employeeDao.selectPage(ip, qw);

            //return employeeDao.selectPage(ip,qw);
            return new P(list,p,ps,tobal,pages);
        }
    }

    @Override
    public int update(Employee employee,HttpSession session) {
        //调用dao层得修改方法将页面传过来得数据进行对数据库进行跟新
        Employee employee1 = (Employee) session.getAttribute("employee");
     /*   employee.setUpdateUser(employee1.getId());
        employee.setUpdateTime(LocalDateTime.now());*/
        return employeeDao.updateById(employee);
    }
}
